/*
 *
 * This is a VoIP Application designed for local-network usage for voice and
 * messaging
 * Copyright (C) 2012  Imran M Yousuf (imyousuf@smitsol.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.smitsol.voip.api;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.smartitengineering.cms.api.common.MediaType;
import com.smartitengineering.cms.api.factory.SmartContentAPI;
import com.smartitengineering.cms.api.factory.type.WritableContentType;
import com.smartitengineering.cms.api.factory.workspace.WorkspaceAPI;
import com.smartitengineering.cms.api.workspace.Workspace;
import com.smartitengineering.cms.api.workspace.WorkspaceId;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;

/**
 *
 * @author imyousuf
 */
@Singleton
public class API {

  public static final String API_CONTEXT_NAME = "com.smitsol.smart-voip.api";
  public static final String INJECTOR_CONTEXT_NAME = "com.smitsol.smart-voip.api.injector";
  private final UserService userService;
  private final BindingService bindingService;
  private final BindingRegistryService bindingRegistryService;
  private final Map<String, String> configs;

  @Inject
  public API(UserService userService, BindingService bindingService, BindingRegistryService bindingRegistryService,
             Map<String, String> configs) {
    this.userService = userService;
    this.bindingService = bindingService;
    this.bindingRegistryService = bindingRegistryService;
    this.configs = configs;
  }

  @Inject
  public void initAPI() {
    final WorkspaceAPI workspaceApi = SmartContentAPI.getInstance().getWorkspaceApi();
    WorkspaceId workspaceId = workspaceApi.createWorkspaceId("com.smitsol", "voip");
    Workspace workspace = workspaceId.getWorkspace();
    if (workspace == null) {
      workspaceApi.createWorkspace(workspaceId);
    }
    try {
      InputStream stream = getClass().getClassLoader().getResourceAsStream(
          "com/smitsol/voip/api/cms/types/content-type.xml");
      if (stream == null) {
        throw new IllegalArgumentException("Content Type XML Does not exist");
      }
      Collection<WritableContentType> types = SmartContentAPI.getInstance().getContentTypeLoader().
          parseContentTypes(
          workspaceId, stream, MediaType.APPLICATION_XML);
      for (WritableContentType type : types) {
        type.put();
      }
    }
    catch (Exception ex) {
      throw new IllegalStateException(ex);
    }

  }

  public BindingService getBindingService() {
    return bindingService;
  }

  public UserService getUserService() {
    return userService;
  }

  public String getConfig(Configurations config) {
    return configs.get(config.getConfigName());
  }

  public String getConfig(String config) {
    return configs.get(config);
  }

  public BindingRegistryService getBindingRegistryService() {
    return bindingRegistryService;
  }
}
